package cn.cjy.servlet;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.cjy.model.Article;
import cn.cjy.service.ArticleService;
import cn.cjy.service.impl.ArticleServiceImpl;



/**
 * Servlet implementation class ArticleServlet
 */
@WebServlet("/ArticleServlet")
public class ArticleServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ArticleServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub

		String ar=request.getParameter("ar");
		switch(ar) {
		case "add":
			response.sendRedirect("static/index.jsp");
			break;
		case "delete":
			response.sendRedirect("static/tip.jsp");
			break;
		case "select":
			
			break;
		default:
			break;
		
		}
		
		response.getWriter().append("Served at: ").append(request.getContextPath());
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub

		request.setCharacterEncoding("utf-8");
		String ar=request.getParameter("ar");
		switch(ar) {
		case "add":
			add(request,response);
			break;
		case "delete":
			delete(request,response);
			break;
		default:
			break;
		
		}
	}

	private void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
		//response.setContentType("text/html;charset=utf-8");
		//request.setCharacterEncoding("utf-8");
		//response.setCharacterEncoding("utf-8");
		String title=request.getParameter("title");
		String content=request.getParameter("content");
		String catalogs=request.getParameter("catalogs");
		String cover=request.getParameter("cover");
		String userinfo=request.getParameter("userinfo");
		String posttime=request.getParameter("posttime");
		String review=request.getParameter("review");
		
		Article article=new Article();
		article.setTitle(title);
		article.setContent(content);
		article.setCatalogs(catalogs);
		article.setCover(cover);
		article.setUserinfo(userinfo);
		
		SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd");
		Date date=null;
		try {
			date = format.parse(posttime);
		} catch (ParseException e) {
			e.printStackTrace();}
		article.setPosttime(date);
		article.setReview(review);
		
		ArticleService articleService=new ArticleServiceImpl();
		int n=articleService.add(article);
		if(n>=1) {
			request.setAttribute("information", "发布成功");
			request.setAttribute("url", "static/index.jsp");
			request.setAttribute("second", 3);
			RequestDispatcher dispatcher=request.getRequestDispatcher("static/tip.jsp");
			dispatcher.forward(request, response);
		}else {
			request.setAttribute("information", "发布失败");
			request.setAttribute("url", "static/add-project.jsp");
			request.setAttribute("second", 3);
			RequestDispatcher dispatcher=request.getRequestDispatcher("static/tip.jsp");
			dispatcher.forward(request, response);
		}
		
		
	}
	
	private void delete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
		
	}
}
